
$(document).ready(function () {
    //初始化标准来源和数据元类型
    initAddData();
    var syjdm = Common.GetRequest().syjdm;
    if(!syjdm){
        $('#zhgxrq').val(getNowFormatDate())
    }else{
        getSjyxxBySjydm(); //根据数据元代码编辑回显数据
    }
});

//初始化标准来源和数据元类型
function initAddData(){
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        data: {zblbdm: '1,2'},
        async: false,
        url: "element/initAddData",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                //表单自动赋值
                if(jsonData.zdbList!=null){
                    $.each(jsonData.zdbList,function(index,obj) {
                        if(obj.zdlbdm=='1'){
                            $("#bzly").append("<option value='" + obj.zddm + "'>" + obj.zdmc + "</option>");
                        }else if(obj.zdlbdm=='2'){
                            $("#sjylx").append("<option value='" + obj.zddm + "'>" + obj.zdmc + "</option>");
                        }
                    })
                }
            }
        }
    });
}

//获取当前时间 作为新增数据的更新时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
    var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
    var currentdate = date.getFullYear() + seperator1  + month  + seperator1  + strDate
        + " "  + date.getHours()  + seperator2  + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}

//关闭窗口
function cancel() {
    parent.layer.closeAll();
}
//把空的节点默认为null
function trim_nulls(data) {
    var y;
    for (var x in data) {
        y = data[x];
        y   =   y.replace(/^\s+|\s+$/g,"");
        if (y ==="") {
            data[x]=null;
        }
    }
    return data;
}
//保存
function  saveSjyxx() {
    var syjdm = Common.GetRequest().syjdm;

    var data = $('form').serializeJson();
    //把""改成null，让后台非空字段报错
    data=trim_nulls(data);
    var url = 'element/saveElement';
    if (syjdm) {
        url = 'element/updateElement';
        data.zhgxrq=getNowFormatDate();
    }
    var reg=/^DE[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9]$/;
    if(!reg.test(data.sjybsf)){
        LayerWn.showFailAlert('数据元标识符格式错误！');
        return
    }
    var formJSON = JSON.stringify(data);
    $.ajax({
        type: "POST",
        contentType: 'application/json',
        dataType: "json",
        url: url,
        data: formJSON,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                if (jsonData.res == "1") {
                    LayerWn.showSucAlert('保存成功！');

                    for (var k = 0; k < parent.frames.length; k++) {
                        if (parent.frames[k].frameElement.src.indexOf("psms-sjygl.html") != -1)
                            parent.frames[k].searchSjyxx();
                    }

                    setTimeout(function () {
                        parent.layer.closeAll()
                    }, 1000);
                } else if (jsonData.res == "-1") {
                    LayerWn.showSucAlert('已存在！');
                }
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });
}

//编辑回显数据
function getSjyxxBySjydm() {
    var sjydm = Common.GetRequest().syjdm; //获取传入的数据元代码
    $("#sjydm").attr("readOnly", "true");
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        data: {sjydm: sjydm},
        cache: false,
        async: false,
        url: "element/querySjyxxBySjydm",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                //表单自动赋值
                $('#sjyxx').populateForm(jsonData.sjyxx)

                //选中
               if(sjydm && jsonData.sjyxx.sjylx!=null){
                    var sjylx=jsonData.sjyxx.sjylx;
                    var numbers = $("#sjylx").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        if ($(numbers[j]).val() == sjylx) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
                if(sjydm && jsonData.sjyxx.bzly!=null){
                    var bzly=jsonData.sjyxx.bzly;
                    var numbers = $("#bzly").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        if ($(numbers[j]).val() == bzly) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
            }
        }
    })
}

//加载标准来源
function loadzytree() {
    LayerWn.openDialog("选择值域表","open-zy.html",500,400);
}

//点击值域树设置数据元有效值
function setSjyyxz(sjyyxz){
    $("#sjyyxz").val(sjyyxz);
}